var refreshGirdData; // 更新数据
var companyId = '';
var ztId = '';
var kjnd = '';
var kjkm = [];
var aj = [];
var pjh = [];
var zph = [];
var wldx = [];
var xm = [];
var bm = [];
var alreadyformat = false;

var userdefault = {};
var params = {};
var bootstrap = function ($, learun) {

    var pageWidth = 1024;
    var colData2 = [];

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            //初始化打印插件
            hiprint.init();
            pageWidth = ($('.lr-layout-grid').width());

            //page.initGrid();
            page.bind();


        },
        bind: function () {
            $('#multiple_condition_query').lrMultipleQuery(function (queryJson) {
                // 调用后台查询
                // queryJson 查询条件
                page.search(queryJson);

            }, 400, 800);
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;

                        $('#zt_select').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'zts/dropdownlist?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId},
                            select: function (ztItem) {
                                if (!ztItem || ztItem.value == '-1') {
                                    ztId = "";
                                } else {
                                    ztId = ztItem.value;
                                    $("#zt_title").html(ztItem.text);
                                    $('#kjnd_select').lrselectRefresh({
                                        // 访问数据接口地址
                                        url: config.base_server() + 'kjnds/dropdownlist?access_token=' + config.getToken().access_token,
                                        // 访问数据接口参数
                                        param: {companyId: companyId, ztId: ztItem.value},
                                    }).lrselectSet(userdefault.kjnd);
                                }
                            }
                        }).lrselectSet(userdefault.ztId);
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 帐套选择
            $('#zt_select').lrselect({
                type: 'default',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 300
            });
            // 会计年度选择
            $('#kjnd_select').lrselect({
                type: 'default',
                //placeholder: '请选择会计年度',
                // 展开最大高度
                maxHeight: 300,
                select: function (item) {
                    if (!item || item.value == '-1') {
                        kjnd = '';
                    } else {
                        kjnd = item.value;

                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-12-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);

                        $('#kjkmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'kjkms/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '科目', name: 'codeAndName', width: 225, align: 'left'},
                                {
                                    label: "余额方向", name: "yefx", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "借" : "贷";
                                    }
                                },
                                {
                                    label: "项目", name: "isXmHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "部门", name: "isBmHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "案件", name: "isAjHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "票据号", name: "isPjhHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "支票号", name: "isZphHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "往来", name: "isWlHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {label: '是否明细', name: 'isMx', width: 1, align: 'left', hidden: true},
                                {label: '编号', name: 'code', width: 120, align: 'left', hidden: true},
                                {label: '简称', name: 'simpleName', width: 225, align: 'left', hidden: true}
                            ],
                            select: function (values, texts) {
                                kjkm = values;
                            }
                        });
                        $('#xmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'xms/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                                {label: '编号', name: 'code', width: 120, align: 'left'},
                            ],
                            select: function (values, texts) {
                                xm = values;
                            }
                        });
                        $('#bmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'departments?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                                {label: '编号', name: 'code', width: 120, align: 'left'},
                            ],
                            select: function (values, texts) {
                                bm = values;
                            }
                        });
                        $('#ajCodes').lrlayerselect({
                            dataValueId: 'name',
                            dataTextId: 'name',
                            reset: true,
                            url: config.base_server() + 'ajs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'name', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                            ],
                            select: function (values, texts) {
                                aj = values;
                            }
                        });
                        $('#pjhCodes').lrlayerselect({
                            dataValueId: 'name',
                            dataTextId: 'name',
                            reset: true,
                            url: config.base_server() + 'pjhs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=1'  + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'name', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                            ],
                            select: function (values, texts) {
                                pjh = values;
                            }
                        });
                        $('#zphCodes').lrlayerselect({
                            dataValueId: 'name',
                            dataTextId: 'name',
                            reset: true,
                            url: config.base_server() + 'pjhs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=2' + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'name', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                            ],
                            select: function (values, texts) {
                                zph = values;
                            }
                        });
                        $('#wldxCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'wldxs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                                {label: '编号', name: 'code', width: 120, align: 'left', hidden: true},
                            ],
                            select: function (values, texts) {
                                wldx = values;
                            }
                        });
                    }
                }
            });


            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            //打印
            $('#lr_print').on('click', function () {
                var hiprintTemplate = null;
                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=fzxyeb', function (res) {
                    if (res.resp_code == 200) {

                        var temp = JSON.parse(res.data.templete);
                        temp.panels[0].printElements[6].options.columns[0] = colData2;
                        var temp2 = JSON.stringify(temp);
                        var temp3 = JSON.parse(temp2);

                        hiprintTemplate = new hiprint.PrintTemplate({
                            template: temp3,
                            paginationContainer: '.hiprint-printPagination'
                        });


                        var rowdatas = $('#gridtable').jfGridGet('rowdatas');
                        var item = {};
                        item.pzflList = rowdatas;
                        if (!alreadyformat) {
                            for (var i = 0; i < item.pzflList.length; i++) {
                                item.pzflList[i].ncye = $.formatMoney(item.pzflList[i].ncye, 2);
                                item.pzflList[i].qcye = $.formatMoney(item.pzflList[i].qcye, 2);
                                item.pzflList[i].jfs = $.formatMoney(item.pzflList[i].jfs, 2);
                                item.pzflList[i].dfs = $.formatMoney(item.pzflList[i].dfs, 2);
                                item.pzflList[i].jlj = $.formatMoney(item.pzflList[i].jlj, 2);
                                item.pzflList[i].dlj = $.formatMoney(item.pzflList[i].dlj, 2);
                                item.pzflList[i].ye = $.formatMoney(item.pzflList[i].ye, 2);
                            }
                        }
                        alreadyformat = true;

                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();
                        item.pzrq = pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10);
                        hiprintTemplate.print(item);
                    }
                });
            });

            //导出Excel
            $("#lr_export_excel").on('click', function () {
                params.access_token = config.getToken().access_token;

                learun.download({
                    url: config.base_server() + 'reports/fzxyeb/excel',
                    param: params
                });
            });
            //导出pdf
            $("#lr_export_pdf").on('click', function () {
                params.access_token = config.getToken().access_token;

                learun.download({
                    url: config.base_server() + 'reports/fzxyeb/pdf',
                    param: params
                });
            });
        },
        initGrid: function (headData) {
            $("#gridtable").height($(window).height() - 170);
            $('#gridtable').jfGridClear();
            $('#gridtable').jfGrid({
                url: config.base_server() + 'reports/fzxyeb?access_token=' + config.getToken().access_token,
                headData: headData,
                reloadSelected: true,
                mainId: 'code'
            });
        },
        search: function (param) {
            param = param || {};
            var pzrqStart = $("#pzrqStart").val();
            var pzrqEnd = $("#pzrqEnd").val();

            param.companyId = companyId;
            param.ztId = ztId;
            param.kjnd = kjnd;
            param.pzrqStart = pzrqStart;
            param.pzrqEnd = pzrqEnd;

            var formData = $('.lr-query-formcontent').lrGetFormData();

            param.kjkmEnable = formData.kjkmEnable;
            param.ajEnable = formData.ajEnable;
            param.pjhEnable = formData.pjhEnable;
            param.zphEnable = formData.zphEnable;
            param.wldxEnable = formData.wldxEnable;
            param.bmEnable = formData.bmEnable;
            param.xmEnable = formData.xmEnable;

            if (formData.kjkmEnable == 0 && formData.ajEnable == 0 && formData.pjhEnable == 0  && formData.zphEnable == 0 && formData.wldxEnable == 0 && formData.bmEnable == 0 && formData.xmEnable == 0) {
                learun.alert.error("至少选择一个辅助项");
                return;
            }

            param.kjkmXh = formData.kjkmXh;
            param.ajXh = formData.ajXh;
            param.pjhXh = formData.pjhXh;
            param.zphXh = formData.zphXh;
            param.xmXh = formData.xmXh;
            param.bmXh = formData.bmXh;
            param.wldxXh = formData.wldxXh;
            if ($.isArray(kjkm)) {
                param.kjkmCodes = kjkm.join();
            }
            if ($.isArray(aj)) {
                param.ajCodes = aj.join();
            }
            if ($.isArray(pjh)) {
                param.pjhCodes = pjh.join();
            }
            if ($.isArray(zph)) {
                param.zphCodes = zph.join();
            }
            if ($.isArray(wldx)) {
                param.wldxCodes = wldx.join();
            }
            if ($.isArray(xm)) {
                param.xmCodes = xm.join();
            }
            if ($.isArray(bm)) {
                param.bmCodes = bm.join();
            }
            params = param;
            learun.httpPost(config.base_server() + 'reports/fzxyeb_header?access_token=' + config.getToken().access_token, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    colData2 = res.data.colData2;
                    for (var i = 0; i < res.data.colData.length; i++) {
                        if (res.data.colData[i].formatter) {
                            res.data.colData[i].formatter = eval("(function(){return " + res.data.colData[i].formatter + " })()");
                        }
                        if (res.data.colData[i].children) {
                            for (var j = 0; j < res.data.colData[i].children.length; j++) {
                                if (res.data.colData[i].children[j].formatter) {
                                    res.data.colData[i].children[j].formatter = eval("(function(){return " + res.data.colData[i].children[j].formatter + " })()");
                                }
                            }
                        }
                    }
                    page.initGrid(res.data.colData);
                } else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
            });
            alreadyformat = false;
            $('#gridtable').jfGridSet('reload', param);
            $("#pzrq_title").html(pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10));
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


